package com.cn.miniDvd;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

/**
 * dvdmgr
 *
 * @author lw
 * @ClassName DVDMgr.java
 * @Description 迷你dvd项目
 * @createTime 2023年05月23日 09:41:00
 */
public class DVDMgr {
    DVDSet dvd = new DVDSet();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    Scanner sc = new Scanner(System.in);
    int n;         //菜单选择输入
    long charge;      //日期差
    private final int RENT=1;    //租金为1元/日

    /**
     * 初始化DVD数据
     */
    public void initDVD() {
        dvd.name[0] = "罗马假日";
        dvd.name[1] = "风声鹤唳";
        dvd.name[2] = "浪漫满屋";
        dvd.state[0] = 0;
        dvd.state[1] = 1;
        dvd.state[2] = 1;
        dvd.date[0] = "2013-7-1";
    }

    /**
     * 菜单切换
     *
     * @throws Exception 异常
     */
    public void startMenu() throws Exception {
        initDVD();
        do {
            returnMain();
            if (sc.hasNextInt()) {
                n = sc.nextInt();
                switch (n) {
                    case 1:
                        addDVD();
                        if (reMain()==0){
                            continue;
                        }else {
                            System.out.println("输入错误！");
                            return;
                        }
                    case 2:
                        search();
                        if (reMain()==0){
                            continue;
                        }else {
                            System.out.println("输入错误！");
                            return;
                        }
                    case 3:
                        deleteDVD();
                        if (reMain()==0){
                            continue;
                        }else {
                            System.out.println("输入错误！");
                            return;
                        }
                    case 4:
                        borrowDVD();
                        if (reMain()==0){
                            continue;
                        }else {
                            System.out.println("输入错误！");
                            return;
                        }
                    case 5:
                        returnDVD();
                        if (reMain()==0){
                            continue;
                        }else {
                            System.out.println("输入错误！");
                            return;
                        }
                    case 6:
                        System.out.println("谢谢使用！DVD已关闭！");
                        return;
                    default:
                        System.out.println("请输入合法的数字(0~6):");
                        break;
                }
            } else {
                sc = new Scanner(System.in);
                System.out.println("输入错误！请输入数字:");
            }
        } while (true);

    }

    /**
     * 主菜单
     */
    public void returnMain() {
        System.out.println("\t\t欢迎使用迷你DVD管理器");
        System.out.println("-----------------------------------------");
        System.out.println("1、新增DVD");
        System.out.println("2、查看DVD");
        System.out.println("3、删除DVD");
        System.out.println("4、借出DVD");
        System.out.println("5、归还DVD");
        System.out.println("6、退出DVD");
        System.out.println("-----------------------------------------");
        System.out.print("请选择：");
    }

    /**
     * 返回主菜单
     * @return int
     */
    public int reMain() {
        System.out.print("输入0返回：");
        n=sc.nextInt();
        return n;
    }

    /**
     * 搜索DVD
     */
    public void search() {
        System.out.println("序号\t\t状态\t\t  名  称\t\t\t 借出日期");
        int i = 0;
        String state;
        String date;
        while (dvd.name[i] != null) {
            if (dvd.state[i] == 1) {
                state = "可 借";
            } else {
                state = "已借出";
            }
            if (dvd.date[i] != null) {
                date = dvd.date[i];
            } else {
                date = "";
            }
            System.out.println(String.format("%-8d",(i + 1)) + String.format("%-8s",state)
                    + String.format("%-8s","《" + dvd.name[i] + "》\t") + String.format("%-8s",date));
            i++;
        }
        System.out.println("-----------------------------------------");
    }

    /**
     * 新增DVD
     */
    public void addDVD() {
        System.out.println("---->  新增DVD：");
        System.out.print("\n请输入DVD名称：");
        String name = sc.next();
        for (int i = 0; i < dvd.name.length; i++) {
            if (dvd.name[i] == null && !name.equals(dvd.name[i])) {
                dvd.name[i] = name;
                dvd.state[i] = 1;
                System.out.println("DVD添加成功！");
                break;
            } else if (name.equals(dvd.name[i])) {
                System.out.println("DVD已存在！");
                break;
            } else if (i == dvd.name.length - 1) {
                System.out.println("DVD已存满，请删除无用DVD后再存入");
            }
        }
        System.out.println("-----------------------------------------");
    }

    /**
     * 删除DVD
     */
    public void deleteDVD() {
        System.out.println("----> 删除DVD：");
        System.out.print("\n请输入要删除的DVD名称：");
        String name = sc.next();
        for (int i = 0; i < dvd.name.length; i++) {
            if (name.equals(dvd.name[i]) && dvd.state[i] == 1) {
                System.out.println("删除《" + dvd.name[i] + "》成功");
                for (int j = i; j < dvd.name.length - 1; j++) {
                    dvd.name[j] = dvd.name[j + 1];
                    dvd.state[j] = dvd.state[j + 1];
                    dvd.date[j] = dvd.date[j + 1];
                    if (dvd.name[j + 1] == null) {
                        dvd.name[j] = null;
                        dvd.date[j] = null;
                        break;
                    }
                }
                break;
            } else if (name.equals(dvd.name[i]) && dvd.state[i] == 0) {
                System.out.println("《" + dvd.name[i] + "》为借出状态" + "不能删除！");
                break;
            } else if (i == dvd.name.length - 1) {
                System.out.println("删除失败！没有找到《" + name + "》");
            }
        }
        System.out.println("-----------------------------------------");
    }

    /**
     * 借出DVD
     */
    public void borrowDVD() {
        System.out.println("----> 借出DVD：");
        System.out.print("\n请输入要借出的DVD名称：");
        String name = sc.next();
        for (int i = 0; i < dvd.name.length; i++) {
            if (name.equals(dvd.name[i]) && dvd.state[i] == 1) {
                System.out.print("请输入借出日期：（年-月-日）：");
                String date = sc.next();
                if (date.indexOf("-") != -1){
                    dvd.date[i] = date;
                    dvd.state[i] = 0;
                    System.out.println("借出《"+dvd.name[i] + "》成功!");
                    break;
                }else {
                    System.out.println("输入的日期格式有误！");
                    break;
                }
            } else if (name.equals(dvd.name[i]) && dvd.state[i] == 0) {
                System.out.println("抱歉！《" + dvd.name[i] + "》" + "已借出！");
                break;
            } else if (i == dvd.name.length - 1) {
                System.out.println("未找到《" + name + "》");
            }
        }
        System.out.println("-----------------------------------------");
    }

    /**
     * 归还DVD
     * @throws Exception 异常
     */
    public void returnDVD() throws Exception {
        System.out.println("----> 归还DVD：");
        System.out.print("\n请输入要归还的DVD名称: ");
        String name = sc.next();
        for (int i = 0; i < dvd.name.length; i++) {
            if (name.equals(dvd.name[i]) && dvd.state[i] == 0) {
                System.out.print("请输入归还日期：（年-月-日）：");
                String date = sc.next();
                if (date.indexOf("-") != -1){
                    Date d1 = sdf.parse(dvd.date[i]);
                    Date d2 = sdf.parse(date);
                    charge= (d2.getTime() - d1.getTime())/(24* 60 *60*1000);
                    int price= (int)(charge * RENT);
                    if (price<0){
                        System.out.println("归还失败！归还日期不能在借出日期之后！");
                        break;
                    }
                    System.out.println("归还《" + dvd.name[i] + "》成功！");
                    System.out.println("借出日期："+dvd.date[i]);
                    System.out.println("归还日期："+date);
                    System.out.println("应付租金（元）："+price + "元");
                    dvd.state[i] = 1;
                    dvd.date[i] = null;
                    break;
                }else {
                    System.out.println("日期格式输入错误！");
                    break;
                }
            } else if (name.equals(dvd.name[i]) && dvd.state[i] == 1) {
                System.out.println("《" + dvd.name[i] + "》" + "未被借出！");
                break;
            } else if (i == dvd.name.length - 1) {
                System.out.println("归还失败！未找到《" + name + "》DVD！");
            }
        }
        System.out.println("-----------------------------------------");
    }
}
